﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 生成验证码图片
{
    public class IdentifyingCode
    {
        /// <summary>
        /// 验证码长度
        /// </summary>
        public static int number = 4;

        /// <summary>
        /// 验证码图片宽度
        /// </summary>
        public static int width = 264;

        /// <summary>
        /// 验证码图片高度
        /// </summary>
        public static int height = 80;

        /// <summary>
        /// 验证码字体类型
        /// </summary>
        public static string fontFamily = "Times New Roman";

        /// <summary>
        /// 验证码字体大小
        /// </summary>
        public static int fontSize = 45;

        /// <summary>
        /// 干扰线数量
        /// </summary>
        public static int countOfVictimLine = 50;

        /// <summary>
        /// 干扰点数量
        /// </summary>
        public static int countOfVictimPoint = 100;

        /// <summary>
        /// 验证码左边距
        /// </summary>
        public static int startX = 40;

        /// <summary>
        /// 验证码上边距
        /// </summary>
        public static int startY = 6;

        /// <summary>
        /// 随机生成若干个字符[a-zA-Z0-9]
        /// </summary>
        /// <returns>验证码</returns>
        private static string CreateCode()
        {
            StringBuilder code = new StringBuilder();

            StringBuilder may = new StringBuilder();

            for (int i = 0; i <= 9; i++)
            {
                may.Append(i);
            }

            for (char i = 'a'; i <= 'z'; i++)
            {
                may.Append(i);
            }

            for (char i = 'A'; i <= 'Z'; i++)
            {
                may.Append(i);
            }

            Random r = new Random();

            for (int i = 0; i < number; i++)
            {
                char c = may[r.Next() % may.Length];
                code.Append(c);
            }

            return code.ToString();
        }

        /// <summary>
        /// 将字符串变成验证码图片
        /// <returns>验证码图片</returns>
        private static Bitmap CreateBitmap(string code)
        {
            try
            {
                Bitmap bitmap = new Bitmap(width, height);
                Graphics graphics = Graphics.FromImage(bitmap);
                graphics.Clear(Color.White);

                Random random = new Random();

                for (int i = 0; i < countOfVictimLine; i++)
                {
                    int x1 = random.Next(bitmap.Width);
                    int y1 = random.Next(bitmap.Height);
                    int x2 = random.Next(bitmap.Width);
                    int y2 = random.Next(bitmap.Height);
                    graphics.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                Font font = new Font(fontFamily, fontSize, FontStyle.Bold | FontStyle.Italic);

                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, bitmap.Width, bitmap.Height), Color.Blue, Color.Red, 1.2f, true);

                graphics.DrawString(code, font, brush, startX, startY);

                for (int i = 0; i < countOfVictimPoint; i++)
                {
                    int x1 = random.Next(bitmap.Width);
                    int y1 = random.Next(bitmap.Height);
                    bitmap.SetPixel(x1, y1, Color.FromArgb(random.Next() % ((int)(Math.Pow(256, 3)))));
                }

                graphics.Dispose();

                return bitmap;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <returns>验证码图片</returns>
        public static Bitmap GetIdentifyingCodeBitmap(out string code)
        {
            code = CreateCode();
            char[] chs = code.ToArray();
            string bitmapCode = string.Join(" ", chs);
            return CreateBitmap(bitmapCode);
        }

    }
}
